package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    @Override
    public List<NursingPlanVo> queryAllNursingPlan() {

        List<NursingPlan> nursingPlans = nursingPlanMapper.selectAll();
        List<NursingPlanVo> collect = nursingPlans.stream().map(list -> BeanUtil.toBean(list, NursingPlanVo.class)).collect(Collectors.toList());
        return collect;
    }

    @Override
    public PageResponse<NursingPlanVo> queryPage(String name, Integer pageNum, Integer pageSize, Integer status) {

        PageHelper.startPage(pageNum,pageSize);
       Page<NursingPlanVo> planVos= nursingPlanMapper.queryPage(name,status);
       return PageResponse.of(planVos,NursingPlanVo.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto,  NursingPlan.class);
       Integer id= nursingPlanMapper.selectByName(nursingPlan.getPlanName());
       if (id!=null){
           throw  new BaseException(BasicEnum.NURSING_PLAN_EXEIST);
       }
       nursingPlanMapper.insert(nursingPlan);


       if ( nursingPlan.getProjectPlans() !=null && !nursingPlan.getProjectPlans().isEmpty()){
           List<NursingProjectPlan> nursingProjectPlans = nursingPlan.getProjectPlans().stream().map(list -> {
               list.setPlanId(nursingPlan.getId());
               return BeanUtil.toBean(list, NursingProjectPlan.class);
           }).collect(Collectors.toList());

           nursingProjectPlanMapper.insert(nursingProjectPlans);
       }

    }

    @Override
    public NursingPlanVo queryById(Integer id) {

     NursingPlan nursingPlan=  nursingPlanMapper.selectById(id);
     return  BeanUtil.toBean(nursingPlan,NursingPlanVo.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void modifyNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        //修改计划信息
        nursingPlanMapper.update(nursingPlan);
        //修改计划绑定的护理项目
        if (!nursingPlan.getProjectPlans().isEmpty()){

            //转换
            List<NursingProjectPlan> nursingProjectPlans = nursingPlan.getProjectPlans().stream().map(list -> {
                list.setPlanId(nursingPlan.getId());
                return BeanUtil.toBean(list, NursingProjectPlan.class);
            }).collect(Collectors.toList());
            //删除原来对应的护理项目
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            //插入新的对应的护理项目
            nursingProjectPlanMapper.insert(nursingProjectPlans);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteNursingPlan(Integer id) {
        //先查看有没有绑定的护理等级 如果有抛异常不准删除
       Integer count= nursingPlanMapper.selectLevelById(id);
       if (count>0){
           throw new BaseException(BasicEnum.NURSING_PLAN_DELETE_FAIL);
       }
       nursingPlanMapper.delete(id);
       nursingProjectPlanMapper.deleteByPlanId(Long.valueOf(id));
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
       NursingPlan nursingPlan=new NursingPlan();
       nursingPlan.setId(Long.valueOf(id));
       nursingPlan.setStatus(status);
        nursingPlanMapper.update(nursingPlan);
    }
}
